import { _decorator, Animation, Component, EventMouse, EventTouch, Input, input, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

export const BLOCK_SIZE = 40

@ccclass('PlayerController')
export class PlayerController extends Component {
    private _startJump: boolean = false //是否为起跳状态
    private _jumpStep: number = 0   //本次起跳几步
    private _curJumpTime: number = 0   //一次完整跳跃过程中已起跳时间
    private _JumpTime: number = 0.1   //完整跳跃所需时间
    private _curJumpSpeed: number = 0 //起跳速度
    private _curPos: Vec3 = new Vec3()   //当前位置
    private _deltaPos: Vec3 = new Vec3(0, 0, 0)  //位移
    private _targetPos: Vec3 = new Vec3   //目标位置
    private _curMoveIndex:number = 0  //用来计分，计算总共跳了几格

    @property(Animation)
    anim: Animation = null  //动画组件获取

    //获取手机触摸按钮
    @property(Node)
    leftTouch:Node = null
    @property(Node)
    rightTouch:Node = null

    //更新画面，非起跳状态不执行，先判断跳跃是否结束，否则以帧为单位移动方块，是则直接唯一至终点
    update(deltaTime: number) {
        if (this._startJump) {
            this._curJumpTime += deltaTime
            if (this._curJumpTime >= this._JumpTime) {
                this.node.setPosition(this._targetPos)
                this._startJump = false
                this.onOnceJumpEnd()
            } else {
                this.node.getPosition(this._curPos)
                this._deltaPos.x = this._curJumpSpeed * deltaTime
                Vec3.add(this._curPos, this._curPos, this._deltaPos)
                this.node.setPosition(this._curPos)
            }
        }

    }

    //根据步数跳跃，方法负责为类的参数赋值，具体位移过程由更新方法实现
    jumpByStep(step: number) {
        if (this._startJump) {
            return
        }
        this._startJump = true
        this._jumpStep = step
        this._curJumpTime = 0

        const state = this.anim.getState('Jump')
        this._JumpTime = state.duration
        this._curJumpSpeed = this._jumpStep * BLOCK_SIZE / this._JumpTime
        this.node.getPosition(this._curPos)
        Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep * BLOCK_SIZE, 0, 0))
        if (this.anim) {
            this.anim.play()
        }
        this._curMoveIndex+=step
    }

    //控制事件监听，状态为游戏中会打开，其余时间关闭
    setInputActive(active:boolean){
        if (active) {
            // input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this)
            this.leftTouch.on(Input.EventType.TOUCH_START,this.onTouchStart,this)
            this.rightTouch.on(Input.EventType.TOUCH_START,this.onTouchStart,this)
        }else{
            // input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this)
            this.leftTouch.off(Input.EventType.TOUCH_START,this.onTouchStart,this)
            this.rightTouch.off(Input.EventType.TOUCH_START,this.onTouchStart,this)
        }
    }

    //重置分数以及位置
    reset(){
        this._curMoveIndex = 0
        this._targetPos.set(0,0,0)
    }

    //监听鼠标
    onMouseUp(event: EventMouse) {
        if (event.getButton() === 0) {
            console.log('前进一步');
            this.jumpByStep(1)
        } else if (event.getButton() === 2) {
            console.log('前进两步');
            this.jumpByStep(2)
        }

    }

    //监听触摸事件
    onTouchStart(event:EventTouch){
        let target = event.target as Node
        if (target.name=="LeftTouch") {
            this.jumpByStep(1)
        }else{
            this.jumpByStep(2)
        }
    }

    //发射自定义事件“一次跳跃结束”
    onOnceJumpEnd(){
        this.node.emit('JumpEnd', this._curMoveIndex)
    }
}


